package com.aishua.fire.controller.sysmanager;

import java.util.List;

import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.menu.AllMenuInfo;
import com.aishua.fire.entity.menu.MenusInfo;
import com.aishua.fire.mapper.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.aishua.fire.common.RequestInterfaceUtil;

import com.aishua.fire.common.anno.PermMapping;
import com.aishua.fire.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@Api(value = "菜单管理相关",tags = {"菜单管理相关接口"})
@RestController
public class SystemMenuController {

	@Autowired
	private MenuService menuService;

	@Autowired
	private ValidateUtil validateUtil;


	@PermMapping(onlyLogin=true)
	@ApiOperation(value="获取左侧导航菜单数据的接口")
	@GetMapping(value = {"/get_leftMenus"} )
	public List<LeftMenuDto> getLeftMenus(){
		return menuService.getLeftMenus();
	}

	@PermMapping(onlyLogin=true)
	@ApiOperation(value="获取二级菜单里所有拥有的接口")
	@GetMapping(value = {"/getTwoLevelMenuPerms/{twoLevelMenuId}"} )
	public List<String> getTwoLevelMenuPerms(@PathVariable("twoLevelMenuId") String twoLevelMenuId){
		return menuService.getTwoLevelMenuPerms(twoLevelMenuId);
	}
	
	/*@PermMapping(onlyLogin=true)
	@ApiOperation(value="获取左侧导航菜单数据的接口")
	@GetMapping(value = {"/getOneLevelMenus"} )
	public List<OneLevelMenu> getOneLevelMenus(){
		
	}*/



	@PermMapping(onlyLogin=true)
	@ApiOperation(value="获取一级和二级菜单")
	@GetMapping(value = {"/getAllMenus"} )
	public List<AllMenuDto>  getMenus(){
		List<AllMenuDto> oneLevelMenus = menuService.getOneLevelMenus();

		for (AllMenuDto allMenuDtos :oneLevelMenus){
			String id=String.valueOf(allMenuDtos.getId());
			List<TwoLevelMenuDto> twoLevelMenus = menuService.getTwoLevelMenus(id);
			allMenuDtos.setTwoLevelMenuDtoList(twoLevelMenus);
		}



		return oneLevelMenus;
	}



	@PermMapping(onlyLogin=true)
	@ApiOperation(value="更新一级和二级菜单")
	@PostMapping(value = {"/add_updateMenu"} )
	public void getOneLevelMenu(@RequestBody @Valid MenusInfo allMenuDtos, BindingResult result) throws ValidateException {

		validateUtil.respErrorMsg(result);
		for(AllMenuInfo oneLevelMenuInfo:allMenuDtos.getAllMenuInfoList()){

			OneLevelMenuDto oneMenuById = menuService.getOneMenuById(oneLevelMenuInfo.getId());
			if(oneMenuById != null){
				//修改
				menuService.updateOneMenu(oneLevelMenuInfo);
			}else{
				//添加
				menuService.addOneMenu(oneLevelMenuInfo);

			}
			List<TwoLevelMenuDto> twoLevelMenuInfos = oneLevelMenuInfo.getTwoLevelMenuDtoList();
			for(TwoLevelMenuDto twoLevelMenuInfo:twoLevelMenuInfos){

				long id= Long.parseLong(twoLevelMenuInfo.getId());
				TwoLevelMenuDto twoMenu = menuService.getTwoMenuById(id);
				if(twoMenu != null){
					//修改
					menuService.updateTwoMenu(twoLevelMenuInfo);
				}else{
					//添加
					menuService.addTwoMenu(twoLevelMenuInfo);
				}
			}
		}
	}







}
